/*
 * Copyright [2011] [Pettersen Consulting]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.   
 */
package com.edb.os.xstream;

import java.util.List;

import com.edb.os.xstream.writer.ClassWriter;
import com.edb.os.xstream.writer.DefaultClassWriter;
import com.edb.os.xstream.writer.EnumValueWriter;
import com.edb.os.xstream.writer.ListWriter;
import com.edb.os.xstream.writer.PrimitiveValueWriter;
import com.edb.os.xstream.writer.StringValueWriter;

/**
 * @author Oystein Pettersen
 *
 */
public class ClassWriterFactory {

    public ClassWriter create(String name, Class<?> clazz, int currentDepth, int paramNameIndex) {

        // check if there is a special provided class writer for this type
        if(boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz)) {
            return new PrimitiveValueWriter(name, clazz, currentDepth, this);
        } else if (clazz.isPrimitive()) {
            return new PrimitiveValueWriter(name, clazz, currentDepth, this);
        } else if (Number.class.isAssignableFrom(clazz)) {
            return new PrimitiveValueWriter(name, clazz, currentDepth, this);
        } else if (clazz.equals(String.class)) {
            return new StringValueWriter(name, clazz, currentDepth, this);
        } else if (clazz.isEnum()) {
            return new EnumValueWriter(name, clazz, currentDepth, this);
        } else if (List.class.isAssignableFrom(clazz)) {
            return new ListWriter(name, clazz, currentDepth, paramNameIndex, this);
        }

        //System.out.printf("%s %s\n", name, clazz);
        return new DefaultClassWriter(name, clazz, currentDepth, paramNameIndex, this);
    }
    
}
